<template>
  <div class="editor-container">
    <div class="agreementEditing">
      <el-icon :size="20" :color="'#000000'" class="icons" @click="closeFun">
        <Close /> </el-icon
      >协议新增
    </div>
    <div style="border: 1px solid #ccc; margin: 0px 20px 20px 20px">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        :mode="mode"
      />
      <Editor
        style="height: 75vh; overflow-y: hidden"
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
      />
    </div>
    <div class="operation">
      <div class="operation-container">
        <el-button type="primary" class="btn" @click="submitFun"
          >确定</el-button
        >
        <el-button class="btn" @click="closeFun">关闭</el-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Close } from "@element-plus/icons-vue";
import "@wangeditor/editor/dist/css/style.css"; // 引入 css
import { onBeforeUnmount, ref, shallowRef, onMounted } from "vue";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
const emit = defineEmits(["close"]);
// 编辑器实例，必须用 shallowRef
const editorRef = shallowRef();
// 内容 HTML
const valueHtml = ref("<p>hello</p>");
const mode = ref("default"); // 或 'simple'
const toolbarConfig = {};
const editorConfig = { placeholder: "请输入内容..." };
// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (editor == null) return;
  editor.destroy();
});
// 关闭弹框
const closeFun = () => {
  emit("close");
};

const submitFun = () => {
  console.log(valueHtml.value);
};

// 提交的数据
const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};
// 模拟 ajax 异步获取内容
onMounted(() => {
  setTimeout(() => {
    valueHtml.value = "<p>模拟 Ajax 异步设置内容</p>";
  }, 1500);
});
</script>

<style lang="scss" scoped>
.editor-container {
  width: 100%;
  height: 100%;
  margin: 0 auto;
  overflow-y: auto;
  .agreementEditing {
    width: calc(100% - 40px);
    text-align: center;
    height: 50px;
    line-height: 50px;
    font-size: 18px;
    margin-left: 20px;
    position: relative;
    .icons {
      cursor: pointer;
      position: absolute;
      left: 0px;
      top: 15px;
      &:hover {
        color: var(--el-color-primary);
      }
    }
  }
  .operation {
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .operation-container {
    width: 220px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .btn {
    width: 100px;
  }
}
</style>
